package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.TemplateFileDao;
import mil.af.amc.adpetracker.model.TemplateFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;


/**
 * Please fill in this Javadoc
 *
 * @author TSgt Ferguson
 */
@Repository("templateFileDao")
public class HibernateTemplateFileDao extends HibernateGenericDao<TemplateFile> implements TemplateFileDao {

    private static final Log log = LogFactory.getLog(HibernateTemplateFileDao.class);

    public HibernateTemplateFileDao() {
        super(TemplateFile.class);
    }

    public TemplateFile getByName(String name) {

        Criteria tfCriteria = sessionFactory.getCurrentSession().createCriteria(TemplateFile.class);
        tfCriteria.add(Restrictions.eq("name", name));

        TemplateFile result = null;
        try {
            result = (TemplateFile) tfCriteria.uniqueResult();
        } catch (NonUniqueResultException ex) {
            log.debug("getByName() returned NonUniqueResultException", ex);
            return null;
        }
        catch (HibernateException e) {
            log.debug("getByName() threw HibernateException", e);
        }

        return result;
//        List<TemplateFile> results =(List<TemplateFile>) getHibernateTemplate().findByCriteria(crit);
//
//        if (results.size() < 1) {
//            return null;
//        }
//
//        return results.get(0);

    }
}
